package com.wushi.lenovo.asia5b.app_ui.other.dialog.shop;

import android.app.Application;
import android.support.annotation.NonNull;

import com.mvp.base.BaseViewModel;
import com.mvp.base.model.ResultModel;
import com.mvp.common.LocalManageUtil;
import com.wushi.lenovo.asia5b.app_config.UserRepository;
import com.wushi.lenovo.asia5b.app_http.ApiService;
import com.wushi.lenovo.asia5b.app_model.other.UserInfo;
import com.wushi.lenovo.asia5b.app_model.wish.GoodsDetailModel;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by zhangyuncai on 2019/5/30.
 */
public class ShopUpdateViewModel extends BaseViewModel {
    private ApiService apiService;

    public ShopUpdateViewModel(@NonNull Application application) {
        super(application);
        apiService = retrofit.create(ApiService.class);
    }

    public void getGoodDetail(int prodId, Consumer<ResultModel<GoodsDetailModel>> consumer) {
        if (!UserRepository.isLogin()) {
            return;
        }
//        if (GlobalValue.isDebug) {
//            prodId = 446847;
//        }
        Disposable subscribe = apiService.getGoodDetail(null, prodId + "", LocalManageUtil.getAppLanguage(), UserInfo.getCountryId() + "")
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .doOnSubscribe(disposable -> {
                    if (loadMvpView != null) {
                        loadMvpView.showLoadingBar(null);
                    }
                })
                .doAfterTerminate(() -> {
                    if (loadMvpView != null) {
                        loadMvpView.dismissLoadingBar("");
                    }
                })
                .subscribe(consumer, throwable -> {
                    throwable.printStackTrace();
                });
        composite.add(subscribe);
    }
}
